package 集合框架;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

//Collections -> List -> LinkedList
public class Demo4 {
    // 1. 如何产生对象
    public static void create() {
        LinkedList<String> list = new LinkedList<>();
        LinkedList<Integer> list1 = new LinkedList<>();
        LinkedList<Integer> list2 = new LinkedList<>(list1);
        System.out.println(list);
        System.out.println(list1);
        System.out.println(list2);
    }

    // 2. 如何对集合进行CRUD(存放、获取、修改、删除)
    public static void crud() {
        LinkedList<String> list = new LinkedList<>();
        //存放
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        list.add("aa");
        System.out.println(list);
        //获取
        String s1 = list.get(1);
        System.out.println(s1);
        //修改
        list.set(0, "ii");
        System.out.println(list);
        //删除
        list.remove("bb");
        list.remove(3);
        System.out.println(list);
        list.clear();
        System.out.println(list);
    }

    // 3. 其他方法
    public static void other() {
        LinkedList<String> list = new LinkedList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        list.add("aa");
        System.out.println(list);
        int size = list.size();
        System.out.println(size);
        boolean b = list.isEmpty();
        System.out.println(b);
        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
    }

    // 4. 循环集合
    public static void loop() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        list.forEach(e -> System.out.print(e+" "));
        System.out.println();
        System.out.println("---------------------------------------");
        for(Integer e : list){
            System.out.print(e+" ");
        }
        System.out.println();
    }

    public static Scanner scanner = new Scanner(System.in);
    // 5. 如何排序、如何查找
    public static void sortAndSearch() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        System.out.print("请输入要查找的数字:");
        Integer key = scanner.nextInt();
        boolean b = list.contains(key);
        int index1 = list.indexOf(key);
        int index2 = list.lastIndexOf(key);
        System.out.println(b);
        System.out.println(index1);
        System.out.println(index2);

        Collections.sort(list);
        System.out.println(list);
    }
    // 6. 特有方法
    public static void special() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 20; i++) {
            list.add((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        list.addFirst(200);
        list.addLast(100);
        System.out.println(list);
        Integer first = list.getFirst();
        Integer last = list.getLast();
        System.out.println(first);
        System.out.println(last);
        list.removeFirst();
        list.removeLast();
        System.out.println(list);
    }

    public static void main(String[] args) {
//        create();
//        crud();
//        other();
//        loop();
//        sortAndSearch();
        special();
    }
}
